/* ----------------------------------------------------------------------------

	pax.cache.template.js Copyright (C) 2006, 2008 Mikkel Bergmann, Pointful

	Licence
	-------
	
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

	See lgpl.txt for licence details

---------------------------------------------------------------------------- */

/*
	Script: pax.cache.template
		This class caches templates in the DOM, and provides convenient methods to manage them.
		
	Author:
		Mikkel Bergmann, <http://www.pointful.com>

*/

var pax = pax || {};
pax.cache = pax.cache || {};
pax.cache.template = pax.cache.template || {};
pax.cache.template.target = document.getElementById('output');		// Default target container. Ie: you need a container with the id of output.


/*	Method: pax.cache.template.loadTemplate
	Retreives a template from a given URL, and caches it. When it is done, it runs the specified call back method
	
	Parameters:
		templateUrl - The URL to load a template from
		postObj - The POST data object, if null or false, we use GET instead. Use {} for no parameters in the POST
		callBack - A method to run when the template has been cached. Hint: the callBack is passed back the URL, which is also the cache key.

	Example:
		(start code)
			<div id="pax.cache.template.loadTemplate.example1"></div>
			[:.
				function hasLoaded( key ) {
					var myData = [
						{	'firstName': 'Bill', 'lastName': 'Washman'	},
						{	'firstName': 'Julia', 'lastName': 'Copalotalus'	},
						{	'firstName': 'Isabella', 'lastName': 'Carnivoree'	}
					];
						
					pax.template.render( pax.cache.get( key ), myData, pax.$('pax.cache.template.loadTemplate.example1') );
					pax.cache.remove( key );
				}
				pax.cache.template.loadTemplate( '/pax/pax.cache.template.loadTemplate.example1.tpl', false, hasLoaded );
			:]
		(end)

		Loads the template into the cache, and then renders it from there, using <pax.template.render>.

	Example:
		(start code)
			<div id="pax.cache.template.loadTemplate.example2"></div>
			[:.
				function hasLoaded( key ) {
					var myData = [
						{	'firstName': 'Bill', 'lastName': 'Washman'	},
						{	'firstName': 'Julia', 'lastName': 'Copalotalus'	},
						{	'firstName': 'Isabella', 'lastName': 'Carnivoree'	}
					];
						
					pax.template.render( pax.cache.get( key ), myData, pax.$('pax.cache.template.loadTemplate.example2') );
					pax.cache.remove( key );
				}
				pax.cache.template.loadTemplate( '/pax/pax.cache.template.loadTemplate.example1.tpl', {}, hasLoaded );
			:]
		(end)

		Loads the template into the cache, (via post with no parameters), and then renders it from there, using <pax.template.render>.
*/
pax.cache.template.loadTemplate = function( templateUrl, postObj, callBack ) {
	if( ! pax.cache.isCached( templateUrl ) ) {
		var callBackFunc = function( xml, txt, url ) {
			pax.cache.set( templateUrl, txt );
			callBack( templateUrl );
		};
		if( postObj )pax.post( templateUrl, postObj, callBackFunc, 'Load POST Template: [' + templateUrl + ']', 'StatusBox' );
		else pax.get( templateUrl, callBackFunc, 'Load GET Template: [' + templateUrl + ']', 'StatusBox' );
	}
	else callBack( templateUrl );
};


/*	Method: pax.cache.template.loadData
	Retreives data from a given URL. When it is done, it runs the specified call back method
	
	Parameters:
		dataUrl - The URL to load data from
		postObj - The POST data object, if null or false, we use GET instead. Use {} for no parameters in the POST
		callBack - A method to run when the template has been cached. Hint: the callBack is passed back the URL, which is also the cache key.

	Example:
		(start code)
			The data loaded is:
			<div id="pax.cache.template.loadData.example1"></div>
			[:.
				function hasLoaded( txt ) {
					pax.$('pax.cache.template.loadData.example1').innerHTML = txt;
				}
				pax.cache.template.loadData( '/pax/pax.cache.template.loadTemplate.example1.data', false, hasLoaded );
			:]
		(end)

		Loads data. Note that data is never cached.
*/
pax.cache.template.loadData = function( dataUrl, postObj, callBack ) {
	var callBackFunc = function( xml, txt, url ) {
		callBack( txt );
	};

	if( postObj )pax.post( dataUrl, postObj, callBackFunc, 'Load POST data: [' + dataUrl + ']', 'StatusBox' );
	else pax.get( dataUrl, callBackFunc, 'Load GET data: [' + dataUrl + ']', 'StatusBox' );
};


/*	Method: pax.cache.template.loadAndRender
	Retreives a template from a given URL, caches it, and then renders it.
	
	Parameters:
		target - the DOM element to render the template and data into.
		templateUrl - The URL to load a template via a GET
		dataUrl - The URL to load data from via a GET
		dataPostObj - If we want to use POST to retreive the data, specify a dictionary of post key -> value pairs here. If you want to use GET, omit this parameter.

	Example:
		(start code)
			<div id="pax.cache.template.loadAndRender.example1"></div>
			[:.
				pax.cache.template.loadAndRender( pax.$('pax.cache.template.loadAndRender.example1'), '/pax/pax.cache.template.loadTemplate.example1.tpl', '/pax/pax.cache.template.loadTemplate.example1.data' );
			:]
		(end)

		Loads the template, then the data, and renders it into the target.
*/
pax.cache.template.loadAndRender = function( target, templateUrl, dataUrl, dataPostObj ) {
	var callBackRenderFunc = function( json ) {
		var data = pax.unJSON( json );
		pax.template.render( pax.cache.get( templateUrl ), data, target );
	};
	
	var dataRenderFunc = function( data ) {
		pax.template.render( pax.cache.get( templateUrl ), data, target );
	};
	
	var callBackDataLoadFunc = function( key ) {
		if( typeof(dataUrl) == 'object' ) {
			dataRenderFunc( dataUrl );
		} else {
			if( dataUrl )pax.cache.template.loadData( dataUrl, dataPostObj, callBackRenderFunc );
			else callBackRenderFunc( "{}" );
		}
	};
	
	pax.cache.template.loadTemplate( templateUrl, false, callBackDataLoadFunc );
};